package com.dayaokeji.rhythmschoolstudent.client.home.course.ranking;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.home.course.ranking.adapter.CourseAllListAdapter;
import com.dayaokeji.rhythmschoolstudent.client.home.course.services.CourseService;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.rhythmschoolstudent.wiget.WrapLinearLayoutManager;
import com.dayaokeji.server_api.domain.Course;
import com.dayaokeji.server_api.domain.CourseTemplate;
import com.dayaokeji.server_api.domain.ScoringRankCourse;
import com.dayaokeji.server_api.domain.UserInfo;

import butterknife.BindView;

public class CourseAllListActivity extends BaseActivity {

    private static final String COURSE_TEMPLATE_ENTITY = "course_tempalte_entity";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_course_name)
    TextView tvCourseName;
    @BindView(R.id.rv_course_all)
    RecyclerView rvCourseAll;

    private CourseTemplate courseTemplate;
    private final CourseAllListAdapter courseAllListAdapter = new CourseAllListAdapter();

    public static void navigation(Context context, CourseTemplate courseTemplate) {
        if (context == null) return;
        Intent intent = new Intent(context, CourseAllListActivity.class);
        intent.putExtra(COURSE_TEMPLATE_ENTITY, courseTemplate);
        ActivityCompat.startActivity(context,intent, null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_all_list);
        setSupportActionBar(toolbar);
        courseTemplate = (CourseTemplate) getIntent().getSerializableExtra(COURSE_TEMPLATE_ENTITY);
        init();
    }

    private void init() {
        setupView();
        setupViewData();
        setOnlistener();
    }

    private void setupView() {
        rvCourseAll.setLayoutManager(new WrapLinearLayoutManager(this));
        rvCourseAll.setAdapter(courseAllListAdapter);
    }

    private void setupViewData() {
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle(courseTemplate.getName()+"-排行");
        }
        tvCourseName.setText(courseTemplate.getName());
        reqCourses();
    }

    private void setOnlistener() {
        courseAllListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (adapter.getData().size() < position) return;
            ScoringRankCourse rankCourse = (ScoringRankCourse) adapter.getData().get(position);
            Course course = new Course();
            course.setCourseDetailId(rankCourse.getDetailId());
            course.setName(rankCourse.getClassName());
            CourseScoringRankingActivity.navigation(CourseAllListActivity.this, course);
        });
    }

    private void reqCourses() {
        UserInfo userInfo = UserUtils.getUserInfo();
        if (userInfo == null) return;
        if (userInfo.getId() == null) return;
        addDisposable(CourseService.INSTANCE.findCourseAllByTempId(courseTemplate.getId(),userInfo.getId()).subscribe(courseAllListAdapter::setNewData));
    }

}
